home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / macforma.0 / macforma / macformat / bin2c.c next >
Encoding:
C/C++ Source or Header  |  1995-03-16  |  2.5 KB  |  107 lines

  1. /***********************************************************
  2. *  bin2c -- convert binary data to C source                *
  3. *----------------------------------------------------------*
  4. *  ⌐1995 Artsoft Development                               *
  5. *        Holger Schemel                                    *
  6. *        33659 Bielefeld-Senne                             *
  7. *        Telefon: (0521) 493245                            *
  8. *        eMail: aeglos@valinor.owl.de                      *
  9. *               aeglos@uni-paderborn.de                    *
  10. *               q99492@pbhrzx.uni-paderborn.de             *
  11. ***********************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #include "global.h"
  17.  
  18. #define COLUMNS        8
  19. #define LINELENGTH    80
  20.  
  21. unsigned char hexvalues[16] =
  22. {
  23.   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  24.   'a', 'b', 'c', 'd', 'e', 'f'
  25. };
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.   int i;
  30.   unsigned char inbyte;
  31.   unsigned char *inptr=&inbyte;
  32.   unsigned long incnt=0;
  33.   char progname[1024],dataname[1024];
  34.   char outstr[LINELENGTH+(2+COLUMNS*6)];
  35.   FILE *infile=stdin, *outfile=stdout;
  36.  
  37.   strcpy(progname,GetFilename(argv[0]));
  38.   strcpy(dataname,"stdin");
  39.  
  40.   if (argc>3 || argc>1 && (!strcmp(argv[1],"-?") || !strcmp(argv[1],"-h")))
  41.   {
  42.     fprintf(stderr,"Usage: %s [input file [output file]]\n", progname);
  43.     exit(-1);
  44.   }
  45.  
  46.   if (argc>1)
  47.   {
  48.     if (!(infile=fopen(argv[1],"r")))
  49.     {
  50.       perror(progname);
  51.       exit(-1);
  52.     }
  53.     strcpy(dataname,GetFilename(argv[1]));
  54.   }
  55.  
  56.   if (argc>2)
  57.   {
  58.     if (!(outfile=fopen(argv[2],"w")))
  59.     {
  60.       perror(progname);
  61.       fclose(infile);
  62.       exit(-1);
  63.     }
  64.     strcpy(dataname,GetFilename(argv[2]));
  65.   }
  66.  
  67.   fprintf(outfile,"/* This is the data of the file '%s' ",
  68.       argc>1 ? argv[1] : "(stdin)");
  69.   fprintf(outfile,"written by 'bin2c' */\n\n");
  70.  
  71.   fprintf(outfile,"unsigned char %s_bytes[] =\n",dataname);
  72.   fprintf(outfile,"{\n");
  73.  
  74.   while(fread(inptr,1,1,infile))
  75.   {
  76.     inbyte = *inptr;
  77.  
  78.     if (!(incnt % COLUMNS))
  79.       fprintf(outfile,"\n  ");
  80.  
  81.     fprintf(outfile,"0x%c%c, ",hexvalues[inbyte>>4],hexvalues[inbyte&15]);
  82.     incnt++;
  83.   }
  84.  
  85.   fprintf(outfile,"\n};\n");
  86.  
  87.   if (!feof(infile))
  88.   {
  89.     fprintf(stderr,"%s: cannot read input file\n", argv[0]);
  90.     fclose(infile);
  91.     fclose(outfile);
  92.     exit(-1);
  93.   }
  94.  
  95.   if (ferror(outfile))
  96.   {
  97.     fprintf(stderr,"%s: cannot write output file\n", argv[0]);
  98.     fclose(infile);
  99.     fclose(outfile);
  100.     exit(-1);
  101.   }
  102.  
  103.   fclose(infile);
  104.   fclose(outfile);
  105.   return(0);
  106. }
  107.